#include "registerform.h"
#include "ui_registerform.h"
#include "widget.h"
#include <QDebug>
#include <QTimer>

registerForm::registerForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registerForm)
{
    ui->setupUi(this);
    m_logManager = new LogManager(this);
    //连接点击btnRegister信号和槽
    connect(ui->btnRegister,&QPushButton::clicked,this,[=](){
        if(ui->lineRegisterUsername->text().isEmpty() ){
            emit sendMessage("用户名为空，请输入有效内容!");
        }else if(ui->lineRegisterPassword->text().isEmpty() ){
            emit sendMessage("密码为空，请输入有效内容!");
        }else if(ui->lineRegisterMail->text().isEmpty() ){
            emit sendMessage("邮箱为空，请输入有效内容!");
        }else if(ui->lineRegisterPhone->text().isEmpty() ){
            emit sendMessage("手机号码为空，请输入有效内容!");
        }else{
            if(m_userMgr.registerUser(ui->lineRegisterUsername->text(),
                                      ui->lineRegisterPassword->text(),
                                      ui->lineRegisterMail->text(),
                                      ui->lineRegisterPhone->text())) {
                emit display(Widget::INDEX_MAINBAR);
                userInfo = m_userMgr.queryUserByUsernamePhoneMail(ui->lineRegisterUsername->text(),"","");
                // 记录操作日志
                if (m_logManager) {
                    m_logManager->recordSystemLog(
                                QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                "OPERATION",                                     // 日志类型
                                "INFO",                                          // 日志级别
                                QString("用户[%1]注册成功!").arg(ui->lineRegisterUsername->text()),   //操作内容
                                userInfo["user_id"].toInt(),               // 用户ID
                            -1                                               // 设备ID
                            );
                }

                // 清空输入框
                ui->lineRegisterUsername->clear();
                ui->lineRegisterPassword->clear();
                ui->lineRegisterMail->clear();
                ui->lineRegisterPhone->clear();
            } else {
                emit sendMessage("用户注册失败:"+m_userMgr.getLastError());
            }
        }
    });

    //连接点击btnGoLogin信号和发送display登录界面(0)信号
    connect(ui->btnGoLogin,&QPushButton::clicked,this,[=](){
        emit display(Widget::INDEX_MAINBAR);
    });
}

registerForm::~registerForm()
{
    delete ui;
}

